using System;
using System.IO;
using System.Net;
using System.Web;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices; 
using System.Data;
using HexLib.Url;

namespace HexLib
{
	/// <summary>
	/// Descrizione di riepilogo per Configura.
	/// </summary>
	public class Configura : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.Label portaProxyLabel;
		public System.Windows.Forms.TextBox passwordProxyTextBox;
		private System.Windows.Forms.Label userNameProxyLabel;
		private System.Windows.Forms.Label serverProxyLabel;
		private System.Windows.Forms.Label passwordProxyLabel;
		public System.Windows.Forms.CheckBox loginProxyCheckBox;
		public System.Windows.Forms.TextBox portaProxyTextBox;
		public System.Windows.Forms.TextBox userNameProxyTextBox;
		public System.Windows.Forms.TextBox domainTextBox;
		private System.Windows.Forms.GroupBox proxyGroupBox;
		public System.Windows.Forms.CheckBox useProxyCheckBox;
		public System.Windows.Forms.TextBox serverProxyTextBox;
		private System.Windows.Forms.GroupBox loginGroupBox;
		private System.Windows.Forms.Label domainLabel;

		private CookieContainer CookieCont = new CookieContainer();
		/// <summary>
		/// Variabile di progettazione necessaria.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Configura()
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent() {
			this.domainLabel = new System.Windows.Forms.Label();
			this.loginGroupBox = new System.Windows.Forms.GroupBox();
			this.domainTextBox = new System.Windows.Forms.TextBox();
			this.passwordProxyLabel = new System.Windows.Forms.Label();
			this.passwordProxyTextBox = new System.Windows.Forms.TextBox();
			this.userNameProxyLabel = new System.Windows.Forms.Label();
			this.userNameProxyTextBox = new System.Windows.Forms.TextBox();
			this.serverProxyTextBox = new System.Windows.Forms.TextBox();
			this.useProxyCheckBox = new System.Windows.Forms.CheckBox();
			this.proxyGroupBox = new System.Windows.Forms.GroupBox();
			this.loginProxyCheckBox = new System.Windows.Forms.CheckBox();
			this.portaProxyLabel = new System.Windows.Forms.Label();
			this.portaProxyTextBox = new System.Windows.Forms.TextBox();
			this.serverProxyLabel = new System.Windows.Forms.Label();
			this.loginGroupBox.SuspendLayout();
			this.proxyGroupBox.SuspendLayout();
			this.SuspendLayout();
			// 
			// domainLabel
			// 
			this.domainLabel.Location = new System.Drawing.Point(8, 77);
			this.domainLabel.Name = "domainLabel";
			this.domainLabel.Size = new System.Drawing.Size(88, 16);
			this.domainLabel.TabIndex = 12;
			this.domainLabel.Text = "Dominio:";
			// 
			// loginGroupBox
			// 
			this.loginGroupBox.Controls.Add(this.domainLabel);
			this.loginGroupBox.Controls.Add(this.domainTextBox);
			this.loginGroupBox.Controls.Add(this.passwordProxyLabel);
			this.loginGroupBox.Controls.Add(this.passwordProxyTextBox);
			this.loginGroupBox.Controls.Add(this.userNameProxyLabel);
			this.loginGroupBox.Controls.Add(this.userNameProxyTextBox);
			this.loginGroupBox.Enabled = false;
			this.loginGroupBox.Location = new System.Drawing.Point(8, 72);
			this.loginGroupBox.Name = "loginGroupBox";
			this.loginGroupBox.Size = new System.Drawing.Size(240, 104);
			this.loginGroupBox.TabIndex = 0;
			this.loginGroupBox.TabStop = false;
			// 
			// domainTextBox
			// 
			this.domainTextBox.Location = new System.Drawing.Point(104, 74);
			this.domainTextBox.Name = "domainTextBox";
			this.domainTextBox.Size = new System.Drawing.Size(128, 20);
			this.domainTextBox.TabIndex = 13;
			this.domainTextBox.Text = "";
			// 
			// passwordProxyLabel
			// 
			this.passwordProxyLabel.Location = new System.Drawing.Point(8, 48);
			this.passwordProxyLabel.Name = "passwordProxyLabel";
			this.passwordProxyLabel.Size = new System.Drawing.Size(88, 16);
			this.passwordProxyLabel.TabIndex = 10;
			this.passwordProxyLabel.Text = "Password:";
			// 
			// passwordProxyTextBox
			// 
			this.passwordProxyTextBox.Location = new System.Drawing.Point(104, 48);
			this.passwordProxyTextBox.Name = "passwordProxyTextBox";
			this.passwordProxyTextBox.PasswordChar = '*';
			this.passwordProxyTextBox.Size = new System.Drawing.Size(128, 20);
			this.passwordProxyTextBox.TabIndex = 11;
			this.passwordProxyTextBox.Text = "";
			// 
			// userNameProxyLabel
			// 
			this.userNameProxyLabel.Location = new System.Drawing.Point(8, 19);
			this.userNameProxyLabel.Name = "userNameProxyLabel";
			this.userNameProxyLabel.Size = new System.Drawing.Size(88, 16);
			this.userNameProxyLabel.TabIndex = 1;
			this.userNameProxyLabel.Text = "Nome Utente:";
			// 
			// userNameProxyTextBox
			// 
			this.userNameProxyTextBox.Location = new System.Drawing.Point(104, 16);
			this.userNameProxyTextBox.Name = "userNameProxyTextBox";
			this.userNameProxyTextBox.Size = new System.Drawing.Size(128, 20);
			this.userNameProxyTextBox.TabIndex = 2;
			this.userNameProxyTextBox.Text = "";
			// 
			// serverProxyTextBox
			// 
			this.serverProxyTextBox.Location = new System.Drawing.Point(104, 16);
			this.serverProxyTextBox.Name = "serverProxyTextBox";
			this.serverProxyTextBox.Size = new System.Drawing.Size(128, 20);
			this.serverProxyTextBox.TabIndex = 4;
			this.serverProxyTextBox.Text = "";
			// 
			// useProxyCheckBox
			// 
			this.useProxyCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.useProxyCheckBox.Location = new System.Drawing.Point(16, 8);
			this.useProxyCheckBox.Name = "useProxyCheckBox";
			this.useProxyCheckBox.Size = new System.Drawing.Size(80, 16);
			this.useProxyCheckBox.TabIndex = 7;
			this.useProxyCheckBox.Text = "Usa proxy";
			this.useProxyCheckBox.CheckedChanged += new System.EventHandler(this.useProxyCheckBox_CheckedChanged);
			// 
			// proxyGroupBox
			// 
			this.proxyGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.proxyGroupBox.Controls.Add(this.loginProxyCheckBox);
			this.proxyGroupBox.Controls.Add(this.portaProxyLabel);
			this.proxyGroupBox.Controls.Add(this.portaProxyTextBox);
			this.proxyGroupBox.Controls.Add(this.serverProxyLabel);
			this.proxyGroupBox.Controls.Add(this.serverProxyTextBox);
			this.proxyGroupBox.Controls.Add(this.loginGroupBox);
			this.proxyGroupBox.Enabled = false;
			this.proxyGroupBox.Location = new System.Drawing.Point(8, 8);
			this.proxyGroupBox.Name = "proxyGroupBox";
			this.proxyGroupBox.Size = new System.Drawing.Size(256, 184);
			this.proxyGroupBox.TabIndex = 10;
			this.proxyGroupBox.TabStop = false;
			// 
			// loginProxyCheckBox
			// 
			this.loginProxyCheckBox.Location = new System.Drawing.Point(16, 70);
			this.loginProxyCheckBox.Name = "loginProxyCheckBox";
			this.loginProxyCheckBox.Size = new System.Drawing.Size(160, 16);
			this.loginProxyCheckBox.TabIndex = 14;
			this.loginProxyCheckBox.Text = "Effettua login sul proxy";
			this.loginProxyCheckBox.CheckedChanged += new System.EventHandler(this.loginProxyCheckBox_CheckedChanged);
			// 
			// portaProxyLabel
			// 
			this.portaProxyLabel.Location = new System.Drawing.Point(8, 48);
			this.portaProxyLabel.Name = "portaProxyLabel";
			this.portaProxyLabel.Size = new System.Drawing.Size(88, 16);
			this.portaProxyLabel.TabIndex = 5;
			this.portaProxyLabel.Text = "Porta Proxy:";
			// 
			// portaProxyTextBox
			// 
			this.portaProxyTextBox.Location = new System.Drawing.Point(104, 48);
			this.portaProxyTextBox.Name = "portaProxyTextBox";
			this.portaProxyTextBox.Size = new System.Drawing.Size(128, 20);
			this.portaProxyTextBox.TabIndex = 6;
			this.portaProxyTextBox.Text = "8080";
			this.portaProxyTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// serverProxyLabel
			// 
			this.serverProxyLabel.Location = new System.Drawing.Point(8, 16);
			this.serverProxyLabel.Name = "serverProxyLabel";
			this.serverProxyLabel.Size = new System.Drawing.Size(88, 16);
			this.serverProxyLabel.TabIndex = 3;
			this.serverProxyLabel.Text = "Server Proxy:";
			// 
			// Configura
			// 
			this.Controls.Add(this.useProxyCheckBox);
			this.Controls.Add(this.proxyGroupBox);
			this.Name = "Configura";
			this.Size = new System.Drawing.Size(272, 200);
			this.loginGroupBox.ResumeLayout(false);
			this.proxyGroupBox.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void useProxyCheckBox_CheckedChanged(object sender, System.EventArgs e)
		{
			proxyGroupBox.Enabled=useProxyCheckBox.Checked;
		}

		private void loginProxyCheckBox_CheckedChanged(object sender, System.EventArgs e)
		{
			loginGroupBox.Enabled=loginProxyCheckBox.Checked;
		}

		public void LoadFromFile(string fileName)
		{
			StringBuilder sb=new StringBuilder(128,128);

			FileIni.GetPrivateProfileString("Proxy","Proxy","0",sb,sb.Capacity,fileName);
			WebHttpQuestioner.UseProxy=useProxyCheckBox.Checked=sb.ToString()=="1"?true:false;
			FileIni.GetPrivateProfileString("Proxy","Server","",sb,sb.Capacity,fileName);
			WebHttpQuestioner.ServerProxy=serverProxyTextBox.Text=sb.ToString();
			FileIni.GetPrivateProfileString("Proxy","Port","8080",sb,sb.Capacity,fileName);
			WebHttpQuestioner.PortProxy=portaProxyTextBox.Text=sb.ToString();

			FileIni.GetPrivateProfileString("Proxy","Login","0",sb,sb.Capacity,fileName);
			WebHttpQuestioner.UseProxyLogin=loginProxyCheckBox.Checked=sb.ToString()=="1"?true:false;
			FileIni.GetPrivateProfileString("Proxy","UserName","",sb,sb.Capacity,fileName);
			WebHttpQuestioner.UserNameProxy=userNameProxyTextBox.Text=sb.ToString();
			FileIni.GetPrivateProfileString("Proxy","Password","",sb,sb.Capacity,fileName);
			WebHttpQuestioner.PasswordProxy=passwordProxyTextBox.Text=sb.ToString();
			FileIni.GetPrivateProfileString("Proxy","Domain","",sb,sb.Capacity,fileName);
			WebHttpQuestioner.DomainProxy=domainTextBox.Text=sb.ToString();
		}

		public void SaveToFile(string fileName)
		{
			FileIni.WritePrivateProfileString("Proxy","Proxy",useProxyCheckBox.Checked?"1":"0",fileName);	
		
			FileIni.WritePrivateProfileString("Proxy","Server",serverProxyTextBox.Text,fileName);
		
			FileIni.WritePrivateProfileString("Proxy","Port",portaProxyTextBox.Text,fileName);

		
			FileIni.WritePrivateProfileString("Proxy","Login",loginProxyCheckBox.Checked?"1":"0",fileName);
		
			FileIni.WritePrivateProfileString("Proxy","UserName",userNameProxyTextBox.Text,fileName);
		
			FileIni.WritePrivateProfileString("Proxy","Password",passwordProxyTextBox.Text,fileName);
		
			FileIni.WritePrivateProfileString("Proxy","Domain",domainTextBox.Text,fileName);
		}
	}
}
